package com.mmall.controller;

import java.util.List;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mmall.common.JsonData;
import com.mmall.common.dtree.DTree;
import com.mmall.common.dtree.DTreeResponse;
import com.mmall.common.dtree.Status;
import com.mmall.model.SysDept;
import com.mmall.param.DeptParam;
import com.mmall.service.SysDeptService;
import com.mmall.service.SysTreeService;

@Controller
@RequestMapping("/sys/dept")
@Slf4j
public class SysDeptController {

    @Resource
    private SysDeptService sysDeptService;
    @Resource
    private SysTreeService sysTreeService;

    @RequestMapping("/dept.page")
    public ModelAndView page() {
        return new ModelAndView("/sys/dept/deptList");
    }
    
    @RequestMapping("/deptSave.page")
    public ModelAndView saveDept() {
        return new ModelAndView("/sys/dept/deptAdd");
    }

    @RequestMapping("/details.page")
    public ModelAndView details() {
        return new ModelAndView("/sys/dept/deptEdit");
    }
    
    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveDept(DeptParam param) {
        sysDeptService.save(param);
        return JsonData.success(null,"新增机构成功");
    }
    
    @RequestMapping("/details.json")
    @ResponseBody
    public JsonData details(@RequestParam("deptId") String deptId) {
        SysDept sysDept = sysDeptService.getDeptById(deptId);
        return JsonData.success(sysDept);
    }

    @RequestMapping("/tree.json")
    @ResponseBody
    public DTreeResponse tree() {
    	DTreeResponse resp = new DTreeResponse();
    	List<DTree> dtoList = sysTreeService.deptTree();
    	resp.setStatus(new Status());
    	resp.setData(dtoList);
    	return resp;
    }

    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateDept(DeptParam param) {
        sysDeptService.update(param);
        return JsonData.success(null,"修改机构成功");
    }

    @RequestMapping("/delete.json")
    @ResponseBody
    public JsonData delete(@RequestParam("id") String id) {
        sysDeptService.delete(id);
        return JsonData.success(null,"删除机构成功");
    }
    
}
